﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MineGoogleTranslator.Core;
using System.Web.UI.HtmlControls;

namespace MineGoogleTranslator.Web
{
	[DefaultProperty("OriginalText")]
	[ToolboxData("<{0}:TranslatorBox runat=server></{0}:TranslatorBox>")]
	public class TranslatorBox : WebControl, INamingContainer
	{
		#region Constants
		private const string CONTAINER_SUFIX = "Container";
		private const string TEXT_SUFIX = "Text";
		private const string BUTTON_SUFIX = "Button";
		private const string RESULT_SUFIX = "Result";
		private const string BUTTON_COMMAND = "Translate";
		#endregion

		#region Events
		public event EventHandler<TranslationEventArgs> Translate;
		#endregion

		#region Properties
		[Bindable(true)]
		[Category("Appearance")]
		[DefaultValue("")]
		[Localizable(true)]
		public string OriginalText
		{
			get
			{
				String s = (String)ViewState["OriginalText"];
				return ((s == null) ? String.Empty : s);
			}

			set
			{
				ViewState["OriginalText"] = value;
			}
		}

		[Bindable(false)]
		[Browsable(false)]
		[Localizable(true)]
		public string TranslatedText
		{
			get
			{
				String s = (String)ViewState["TranslatedText"];
				return ((s == null) ? String.Empty : s);
			}

			set
			{
				ViewState["TranslatedText"] = value;
			}
		}

		[Bindable(true)]
		[Category("Behavior")]
		[DefaultValue("")]
		[Localizable(true)]
		public Language FromLanguage
		{
			get
			{
				Language s = (Language)ViewState["FromLanguage"];
				return ((s == null) ? Language.en : s);
			}

			set
			{
				ViewState["FromLanguage"] = value;
			}
		}

		[Bindable(true)]
		[Category("Behavior")]
		[DefaultValue("")]
		[Localizable(true)]
		public Language ToLanguage
		{
			get
			{
				Language s = (Language)ViewState["ToLanguage"];
				return ((s == null) ? Language.en : s);
			}

			set
			{
				ViewState["ToLanguage"] = value;
			}
		}

		[Bindable(true)]
		[Category("Behavior")]
		[DefaultValue("")]
		[Localizable(true)]
		public TextBoxMode TextMode
		{
			get
			{
				return (ViewState["TextMode"] == null) ? TextBoxMode.SingleLine : (TextBoxMode)ViewState["TextMode"];
			}

			set
			{
				ViewState["TextMode"] = value;
			}
		}

		[Bindable(true)]
		[Category("Appearance")]
		[DefaultValue("")]
		[Localizable(true)]
		public int Rows
		{
			get
			{
				object s = (object)ViewState["Rows"];
				return ((s == null) ? 1 : int.Parse(s.ToString()));
			}

			set
			{
				ViewState["Rows"] = value;
			}
		}

		[Bindable(true)]
		[Category("Appearance")]
		[DefaultValue("")]
		[Localizable(true)]
		public bool ButtonAsHiperlink
		{
			get
			{
				object s = (object)ViewState["ButtonAsHiperlink"];
				return ((s == null) ? false : bool.Parse(s.ToString()));
			}

			set
			{
				ViewState["ButtonAsHiperlink"] = value;
			}
		}

		[Bindable(true)]
		[Category("Appearance")]
		[DefaultValue("")]
		[Localizable(true)]
		public string ButtonText
		{
			get
			{
				String s = (String)ViewState["ButtonText"];
				return ((s == null) ? String.Empty : s);
			}

			set
			{
				ViewState["ButtonText"] = value;
			}
		}

		[Bindable(true)]
		[Category("Appearance")]
		[DefaultValue("")]
		[Localizable(true)]
		public string TextCssClass
		{
			get
			{
				String s = (String)ViewState["TextCssClass"];
				return ((s == null) ? String.Empty : s);
			}

			set
			{
				ViewState["TextCssClass"] = value;
			}
		}

		[Bindable(true)]
		[Category("Appearance")]
		[DefaultValue("")]
		[Localizable(true)]
		public string ButtonCssClass
		{
			get
			{
				String s = (String)ViewState["ButtonCssClass"];
				return ((s == null) ? String.Empty : s);
			}

			set
			{
				ViewState["ButtonCssClass"] = value;
			}
		}

		[Bindable(true)]
		[Category("Appearance")]
		[DefaultValue("")]
		[Localizable(true)]
		public string ResultCssClass
		{
			get
			{
				String s = (String)ViewState["ResultCssClass"];
				return ((s == null) ? String.Empty : s);
			}

			set
			{
				ViewState["ResultCssClass"] = value;
			}
		}
		#endregion

		#region Methods
		private Control CreateResultControl()
		{
			Label result = new Label();
			result.ID = ID + RESULT_SUFIX;
			result.CssClass = ResultCssClass;
			return result;
		}

		private Control CreateButtonControl()
		{
			IButtonControl button = new Button();
			if (ButtonAsHiperlink)
			{
				button = new LinkButton();
				((LinkButton)button).ID = ID + BUTTON_SUFIX;
			}
			else
				((Button)button).ID = ID + BUTTON_SUFIX;

			button.Text = ButtonText;
			button.CommandName = BUTTON_COMMAND;
			((WebControl)button).CssClass = ButtonCssClass;
			return button as WebControl;
		}

		private TextBox CreateTextControl()
		{
			TextBox text = new TextBox();
			text.ID = ID + TEXT_SUFIX;
			text.TextMode = TextMode;
			text.Rows = Rows;
			text.Text = OriginalText;
			text.CssClass = TextCssClass;
			return text;
		}

		private Panel CreatePanelContainer()
		{
			Panel container = new Panel();
			container.ID = ID + CONTAINER_SUFIX;
			container.CssClass = CssClass;
			return container;
		}
		#endregion

		#region Overriden Methods
		protected override void CreateChildControls()
		{
			Controls.Clear();
			Panel container = CreatePanelContainer();
			container.Controls.Add(CreateTextControl());
			container.Controls.Add(CreateButtonControl());
			container.Controls.Add(CreateResultControl());
			Controls.Add(container);
		}

		protected override void OnInit(EventArgs e)
		{
			base.EnsureChildControls();
			base.OnInit(e);
		}

		protected override bool OnBubbleEvent(object source, EventArgs args)
		{
			if (source is IButtonControl && ((IButtonControl)source).CommandName.Equals(BUTTON_COMMAND))
			{
				TextBox text = FindControl(ID + TEXT_SUFIX) as TextBox;
				Label result = FindControl(ID + RESULT_SUFIX) as Label;
				result.Text = Translator.Translate(new LanguagePair(FromLanguage, ToLanguage), text.Text);
				if (Translate != null)
					Translate(this, new TranslationEventArgs(result.Text));
			}
			return base.OnBubbleEvent(source, args);
		}

		protected override void AddAttributesToRender(HtmlTextWriter writer)
		{
			base.AddAttributesToRender(writer);
			//TODO: Verify is it works (remove the css class from this control).
			writer.AddAttribute(HtmlTextWriterAttribute.Class, string.Empty);
		}
		#endregion
	}
}
